package com.feishi.core.util.pinyin;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * Created by caixq on 6/15 015.
 */
public class PinyinUtil {

    private static String toHanyuPinyin(String str) {
        HanyuPinyinOutputFormat format= new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//        format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            try {
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(str.charAt(i), format);
                sb.append(pinyinArray==null?str.charAt(i):(pinyinArray[0].substring(0, 1).toUpperCase() + pinyinArray[0].substring(1)));
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        System.out.println(toHanyuPinyin("打哈苏"));
    }
}
